package vip.finance.makemoney;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Mapper;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.Map;

@SpringBootTest
public class CodeGenerator {

    @Resource
    private DataSourceProperties dataSourceProperties;

    @Test
    void genCode() {
        String projectBaseDir = System.getProperty("user.dir");
        FastAutoGenerator.create(dataSourceProperties.getUrl(), dataSourceProperties.getUsername(), dataSourceProperties.getPassword())
                .globalConfig(builder -> {
                    builder.author("yangbin") // 设置作者
                            //.enableSwagger() // 开启 swagger 模式
                            .disableServiceInterface()
                            .outputDir(projectBaseDir + "/src/main/java"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("vip.finance.makemoney") // 设置父包名
                            .pathInfo(Map.of(OutputFile.xml, projectBaseDir + "/src/main/resources/mapper",
                                    OutputFile.controller, projectBaseDir + "/target/controller"//输出到垃圾堆
                            )); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder//.addInclude("user_stock") // 设置需要生成的表名 不写默认全部表
                            .addTablePrefix("t_", "c_"); // 设置过滤表前缀
                    builder.entityBuilder().enableTableFieldAnnotation().enableLombok()
                            .mapperBuilder().mapperAnnotation(Mapper.class)
                            .controllerBuilder().enableHyphenStyle().enableRestStyle().superClass("vip.finance.makemoney.controller.BaseController")
                            .build();

                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }
}
